package com.lishui.waterfriend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lishui.waterfriend.mapper.TagClassificationMapper;
import com.lishui.waterfriend.mapper.TagMapper;
import com.lishui.waterfriend.pojo.entity.Tag;
import com.lishui.waterfriend.pojo.entity.TagClassification;
import com.lishui.waterfriend.pojo.vo.TagClassificationVO;
import com.lishui.waterfriend.pojo.vo.TagVO;
import com.lishui.waterfriend.service.TagClassificationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 24197
* @description 针对表【tag_classification(标签分裂)】的数据库操作Service实现
* @createDate 2024-04-19 21:01:01
*/
@Service
public class TagClassificationServiceImpl extends ServiceImpl<TagClassificationMapper, TagClassification>
    implements TagClassificationService {

    @Resource
    private TagMapper tagMapper;

    @Override
    public List<TagClassificationVO> getClassVoList() {
        List<TagClassificationVO> tagClassificationVOList = new ArrayList<>();
        List<TagClassification> tagClassificationList = this.list();
        tagClassificationList.forEach(tagClassification -> {
            Long parentId = tagClassification.getId();
            QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
            tagQueryWrapper.eq("parent_id", parentId);
            List<Tag> tags = tagMapper.selectList(tagQueryWrapper);
            List<TagVO> tagVOS = tags.stream().map(tag -> {
                TagVO tagVO = new TagVO();
                tagVO.setId(tag.getTagName());
                tagVO.setText(tag.getTagName());
                tagVO.setParentId(parentId);
                return tagVO;
            }).collect(Collectors.toList());
            // 创建分类VO加入列表
            TagClassificationVO tagClassificationVO = new TagClassificationVO();
            tagClassificationVO.setId(tagClassification.getId());
            tagClassificationVO.setText(tagClassification.getClassName());
            tagClassificationVO.setChildren(tagVOS);
            tagClassificationVOList.add(tagClassificationVO);
        });
        return tagClassificationVOList;
    }
}




